#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVBoxLayout>
#include "ganttchart.h"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setFixedSize(1000,800);
    setWindowTitle("图表案例分析");
    //ui->pushButton_5->setVisible(false);
    CreateBarCharts();
    //普通图
    ui->hlayout_2->addWidget(m_chartview);
    //甘特图
    //ui->hlayout_2->addWidget(m_ganteChartView);
    //QVBoxLayout* vlayout = new QVBoxLayout(this);
    //vlayout->addLayout(ui->hlayout_1);
    //QHBoxLayout* hlayout = new QHBoxLayout(this);
    //hlayout->addLayout(ui->vlayout_1);
    //vlayout->addLayout(ui->vlayout_1);
    //QVBoxLayout* vlayout2 = new QVBoxLayout(this);
    //vlayout2->addWidget(m_chartview);
    //hlayout->addLayout(vlayout2);
    //this->setLayout(hlayout);
    //ui->centralwidget->setLayout(vlayout);
    //this->setLayout(vlayout);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::CreateLineCharts(){
    m_chartview = new QChartView(this);
    m_chart = new QChart();
    m_chart->setTitle("折线图测试");

    m_chartview->setChart(m_chart);
    QLineSeries* series1 = new QLineSeries();
    series1->append(0, 6);
    series1->append(1, 4);
    series1->append(2, 8);
    series1->append(3, 4);
    series1->append(4, 7);
    series1->append(5, 10);
    m_chart->addSeries(series1);
    m_chart->createDefaultAxes();
    m_chart->setAnimationOptions(QChart::SeriesAnimations);
}

void MainWindow::CreateSinCharts(){
    m_chartview = new QChartView(this);
    m_chart = new QChart();
    m_chart->setTitle("sin/cos图表测试");

    m_chartview->setChart(m_chart);
    //this->setCentralWidget(m_chartview);

    QLineSeries * series1 = new QLineSeries();
    QLineSeries * series2 = new QLineSeries();
    series1->setName("sin曲线");
    series2->setName("cos曲线");
    m_chart->addSeries(series1);
    m_chart->addSeries(series2);

    //通过序列添加数值
    qreal t = 0,y1,y2,intv = 0.1;
    int icount = 100;
    for(int i = 0;i < icount;i++){
        y1 = qSin(t);
        series1->append(t,y1);

        y2 = qSin(t +20);
        series2->append(t,y2);

        t = t + intv;
    }

    //创建坐标轴
    QValueAxis *axisX = new QValueAxis;
    axisX->setRange(0,10);
    axisX->setTitleText("time(secs)");

    QValueAxis *axisY = new QValueAxis;
    axisY->setRange(-2,2);
    axisY->setTitleText("values");

    m_chart->setAxisX(axisX,series1);
    m_chart->setAxisY(axisY,series1);

    m_chart->setAxisX(axisX,series2);
    m_chart->setAxisY(axisY,series2);
}


void MainWindow::CreateBarCharts(){
    m_chartview = new QChartView(this);
    m_chart = new QChart();
    m_chart->setTitle("柱状图");
    m_chartview->setChart(m_chart);

    QBarSeries* series = new QBarSeries();
    QBarSet* set0 = new QBarSet("Jane");
    *set0 << 1 << 2 << 3 << 4 << 5;
    series->append(set0);

    QBarSet* set1 = new QBarSet("John");
    *set1 << 5 << 1 << 3 << 2 << 6;
    series->append(set1);

    QBarSet* set2 = new QBarSet("Axel");
    *set2 << 3.3 << 8.2 << 6.3 << 9.8 << 4.5;
    series->append(set2);

    m_chart->addSeries(series);
    m_chart->createDefaultAxes();
    m_chart->legend()->setVisible(true);
    m_chart->legend()->setAlignment(Qt::AlignBottom);
}

void MainWindow::CreateGanteCharts(){
    m_ganteChartView = new GanttChart(this);
    m_ganteChartView->addTask("Task 1",0,10,Qt::blue);
    m_ganteChartView->addTask("Task2",12,7,Qt::green);
}
